/**
 * a script for the user info page
 */

$(document).ready(function(){
	
	$( "#accordion" ).accordion();
	/*
	 * for now, with no change to user name
	 */
	$(".editable2").editable({
								submit:'save',
								cancel:'cancel',
								onSubmit:checkPwd
							 });
	$(".editable3").editable({
								submit:'save',
								cancel:'cancel',
								onSubmit:checkString
							 });
	$(".editable4").editable({
								submit:'save',
								cancel:'cancel',
								onSubmit:checkString
							});
	
	$(function() {
		$("button",".mycourses").button();
		$("button",".mycourses").click(function(){
			if (this.id=="coursepage"){
				var coursename = this.name;
				window.location.href = "/salonmorandsmigi/course.jsp?name="+coursename;
			}
			else {
				var name = $(this).attr('name');
				var username = $(".user").attr('name');
				  $("#dialog").dialog({ buttons: [
				                                   {
				                                       text: "Ok",
				                                       click: function() { 
				                                    	   $.ajax("/salonmorandsmigi/UserServlet?action=unregisterusercourse&coursename="+name+"&username="+username,{
				                                    		   type: "GET",
				                                    		   success: function(){
				                                    		     alert("succeeded deleting");
				                                    		     window.location.replace("/salonmorandsmigi/student/mycourses.jsp");
				                                    		   },
				                                    		   error: function(){
				                                    			 alert("error deleting");  
				                                    		   }
				                                    		 });
				                                    	   $(this).dialog("close");
				                                       }
				                                   },
				                                   {
				                                	   text: "Cancel",
				                                	   click: function() { $(this).dialog("close");}
				                                   }
				                                  ], 
				  						show: "fold",
				  						hide: "explode"
				  					 });
			}		
		});

	});
	
	/**
	 * edit part of the user info script
	 */
	
	function end(content){
		alert(content);
	}
	
	function checkString(content){
		if (this.attr('class')=="editable3"){
			if (content.current==""){
				$(".editable3").html(content.previous);
				$(".editable3").append("<div class=\"fail\">bad input!</div>");
				$(".fail").delay(600).fadeOut(400);
				setTimeout("$(\".fail\").remove()",1000);
				return;
			}
			$.ajax({
					url:"/salonmorandsmigi/UserServlet?action=edit&fullname="+content.current,
					type:"GET",
					success: function(){
						$(".editable3").html(content.current);
						$(".editable3").append("<div class=\"success\">edit succeeded!</div>");
						$(".success").delay(600).fadeOut(400);
						setTimeout("$(\".success\").remove()",1000);
					},
					error: function(){
						alert("error in editing name");
						$(".editable3").html(content.previous);
					}
				  });
		}
		else{
			var phone = Number(content.current);
			if (content.current=="" || isNaN(phone)){
				$(".editable4").html(content.previous);
				$(".editable4").append("<div class=\"fail\">bad input!</div>");
				$(".fail").delay(600).fadeOut(400);
				setTimeout("$(\".fail\").remove()",1000);
				return;
			}
			$.ajax({
				url:"/salonmorandsmigi/UserServlet?action=edit&phone="+content.current,
				type:"GET",
				success: function(){
					$(".editable4").html(content.current);
					$(".editable4").append("<div class=\"success\">edit succeeded!</div>");
					$(".success").delay(600).fadeOut(400);
					setTimeout("$(\".success\").remove()",1000);
				},
				error: function(){
					alert("error in editing phone number");
					$(".editable4").html(content.previous);
				}
			  });
		}
	}
	
	function checkPwd(content){
		if (content.current==""){
			$(".editable2").html(content.previous);
			$(".editable2").append("<div class=\"fail\">bad input!</div>");
			$(".fail").delay(600).fadeOut(400);
			setTimeout("$(\".fail\").remove()",1000);
			return;
		}
		$.ajax({
			url:"/salonmorandsmigi/UserServlet?action=edit&pwd="+content.current,
			type:"GET",
			success: function(){
				$(".editable2").html(content.current);
				$(".editable2").append("<div class=\"success\">edit succeeded!</div>");
				$(".success").delay(600).fadeOut(400);
				setTimeout("$(\".success\").remove()",1000);
			},
			error: function(){
				alert("error in editing password");
				$(".editable2").html(content.previous);
			}
		  });
	}	
});


